package com.cmccgz.udacs_landlord_webapp.main.accreditmanager;

import android.support.annotation.NonNull;
import android.text.TextUtils;

import com.cmccgz.udacs_landlord_webapp.base.ApiService;
import com.cmccgz.udacs_landlord_webapp.bean.CardInfo;
import com.cmccgz.udacs_landlord_webapp.bean.ResultBean;

import java.util.List;

import rx.Subscriber;

/**
 * APP授权Presenter
 */
public class AccreditMngPresenter implements AccreditMngContract.Presenter {

    private AccreditMngContract.View mCardAccreditView;

    public AccreditMngPresenter(@NonNull AccreditMngContract.View cardAccreditView) {
        mCardAccreditView = cardAccreditView;
        mCardAccreditView.setPresenter(this);
    }

    @Override
    public void start() {

    }

    @Override
    public void completeAppAccredit(String mobile, String houseId, String endTime, String userId, List<CardInfo> cardList, String appOpen) {

        if (TextUtils.isEmpty(mobile)) {
            //TODO
            return;
        }
        if (TextUtils.isEmpty(houseId)) {
            mCardAccreditView.showEmptyHouse();
            return;
        }
        if (TextUtils.isEmpty(endTime)) {
            mCardAccreditView.showEmptyEndTime();
            return;
        }
        if(TextUtils.isEmpty(userId)){
            mCardAccreditView.showEmptyUserId();
            return;
        }
        if(cardList==null||cardList.size()==0){
            mCardAccreditView.appAccreditError("请选择卡片!");
            return;
        }
        mCardAccreditView.showLoadingDialog();
        //调接口授权
        //...
        ApiService.appAccredit(mobile,houseId,endTime,userId,cardList,appOpen).subscribe(new Subscriber<ResultBean>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                mCardAccreditView.appAccreditError("授权失败，请稍后再试！");
            }

            @Override
            public void onNext(ResultBean resultBean) {
                if ("0".equals(resultBean.rsmsg.rscode)) {
                    mCardAccreditView.appAccreditCardSuccess();
                }else{
                    mCardAccreditView.appAccreditError(resultBean.rsmsg.rsdesc);
                }
            }
        });
    }


}
